<?php



namespace generate\field;

class SelectTags extends Field
{
    public static $html = <<<EOF
  
    <div class="form-group">
    <label for="FIELD_NAME" class="col-sm-2 control-label"></label>
        <div class="col-sm-10 col-md-4">
            <select name="FIELD_NAME[]" id="FIELD_NAME" multiple="multiple" class="form-control field-select"
                data-placeholder="请选择">
                <option value="">请选择</option>
                {foreach name='' item='item'}
                    <option value="{\$item.id}" {if isset(\$data) && in_array(\$item.id,\$data.FIELD_NAME)}selected{/if}>
                {\$item.name}
                </option>
                {/foreach}

            </select>
        </div>
    </div>
    <script>
        $('#FIELD_NAME').select2({
            tags: true,
            maximumSelectionLength: 3 //最多选择数量
        });
    </script>

\n
EOF;


 

    //多图上传模型内修改器与获取器
    public static $modelAttrCode = <<<EOF
                //[FORM_NAME]获取器
                public function get[FIELD_NAME]Attr(\$value)
                {
                    return explode(',', \$value);
                }
                //[FORM_NAME]修改器
                public function set[FIELD_NAME]Attr(\$value)
                {
                    return implode(',', \$value);
                }
            \n
            EOF;


    public static $rules = [
        'required'   => '非空',
        'FIELD_NAME_size'  => '文件大小限制',
        'FIELD_NAME_image' => '图片类型',
        'regular'    => '自定义正则'
    ];


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace('[FORM_NAME]', $data['form_name'], $html);
        $html = str_replace('[FIELD_NAME]', $data['field_name'], $html);
        return $html;
    }
}
